/*
 * @lc app=leetcode.cn id=61 lang=javascript
 *
 * [61] 旋转链表
 */

// @lc code=start
/**
 * Definition for singly-linked list.
 * function ListNode(val, next) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.next = (next===undefined ? null : next)
 * }
 */
/**
 * @param {ListNode} head
 * @param {number} k
 * @return {ListNode}
 */

var rotateRight = function(head, k) {
    if (!head || !head.next || k === 0) return head;
    let L = 1;
    let cur = head;
    while(cur.next) {
        cur = cur.next;
        L++;
    }
    let removeNum = L - k % L;
    if (removeNum === L) {
        return head;
    }
    cur.next = head;
    while(removeNum) {
        cur = cur.next;
        removeNum--;
    }
    const res = cur.next;
    cur.next = null;
    return res;
};
// @lc code=end

